package org.jeecg.modules.basic.businessparam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basic.businessparam.entity.WmsBusiniessParamConfig;
import org.jeecg.modules.basic.businessparam.service.IWmsBusiniessParamConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 系统业务参数配置
 * @Author: jeecg-boot
 * @Date: 2024-01-17
 * @Version: V1.0
 */
@Api(tags = "系统业务参数配置")
@RestController
@RequestMapping("/businessparam/wmsBusiniessParamConfig")
@Slf4j
public class WmsBusiniessParamConfigController extends JeecgController<WmsBusiniessParamConfig, IWmsBusiniessParamConfigService> {
    @Autowired
    private IWmsBusiniessParamConfigService wmsBusiniessParamConfigService;

    /**
     * 分页列表查询
     *
     * @param wmsBusiniessParamConfig
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "系统业务参数配置-分页列表查询")
    @ApiOperation(value = "系统业务参数配置-分页列表查询", notes = "系统业务参数配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsBusiniessParamConfig>> queryPageList(WmsBusiniessParamConfig wmsBusiniessParamConfig,
                                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                HttpServletRequest req) {
        QueryWrapper<WmsBusiniessParamConfig> queryWrapper = QueryGenerator.initQueryWrapper(wmsBusiniessParamConfig, req.getParameterMap());
        Page<WmsBusiniessParamConfig> page = new Page<WmsBusiniessParamConfig>(pageNo, pageSize);
        IPage<WmsBusiniessParamConfig> pageList = wmsBusiniessParamConfigService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmsBusiniessParamConfig
     * @return
     */
    @AutoLog(value = "系统业务参数配置-添加")
    @ApiOperation(value = "系统业务参数配置-添加", notes = "系统业务参数配置-添加")
    @RequiresPermissions("businessparam:wms_businiess_param_config:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmsBusiniessParamConfig wmsBusiniessParamConfig) {
        wmsBusiniessParamConfigService.save(wmsBusiniessParamConfig);
        wmsBusiniessParamConfigService.addKey(wmsBusiniessParamConfig.getCode(), wmsBusiniessParamConfig.getVal());
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmsBusiniessParamConfig
     * @return
     */
    @AutoLog(value = "系统业务参数配置-编辑")
    @ApiOperation(value = "系统业务参数配置-编辑", notes = "系统业务参数配置-编辑")
    @RequiresPermissions("businessparam:wms_businiess_param_config:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsBusiniessParamConfig wmsBusiniessParamConfig) {
        wmsBusiniessParamConfigService.updateById(wmsBusiniessParamConfig);
        wmsBusiniessParamConfigService.flushConfigParam(wmsBusiniessParamConfig.getCode());
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "系统业务参数配置-通过id删除")
    @ApiOperation(value = "系统业务参数配置-通过id删除", notes = "系统业务参数配置-通过id删除")
    @RequiresPermissions("businessparam:wms_businiess_param_config:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        WmsBusiniessParamConfig config = wmsBusiniessParamConfigService.getById(id);
        wmsBusiniessParamConfigService.flushConfigParam(config.getCode());
        wmsBusiniessParamConfigService.removeById(id);

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "系统业务参数配置-批量删除")
    @ApiOperation(value = "系统业务参数配置-批量删除", notes = "系统业务参数配置-批量删除")
    @RequiresPermissions("businessparam:wms_businiess_param_config:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<WmsBusiniessParamConfig> list = wmsBusiniessParamConfigService.list(new LambdaQueryWrapper<WmsBusiniessParamConfig>().in(WmsBusiniessParamConfig::getId, ids));
        list.stream().map(t -> t.getCode()).forEach(t -> {
            wmsBusiniessParamConfigService.flushConfigParam(t);
        });
        this.wmsBusiniessParamConfigService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "系统业务参数配置-通过id查询")
    @ApiOperation(value = "系统业务参数配置-通过id查询", notes = "系统业务参数配置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsBusiniessParamConfig> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsBusiniessParamConfig wmsBusiniessParamConfig = wmsBusiniessParamConfigService.getById(id);
        if (wmsBusiniessParamConfig == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsBusiniessParamConfig);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsBusiniessParamConfig
     */
    @RequiresPermissions("businessparam:wms_businiess_param_config:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsBusiniessParamConfig wmsBusiniessParamConfig) {
        return super.exportXls(request, wmsBusiniessParamConfig, WmsBusiniessParamConfig.class, "系统业务参数配置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("businessparam:wms_businiess_param_config:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsBusiniessParamConfig.class);
    }


    /**
     * 刷新缓存
     */
    @ApiOperation(value = "清理业务参数缓存", notes = "清理业务参数缓存")
    @RequestMapping(value = "/flushConfigParam", method = RequestMethod.GET)
    public Result<?> flushConfigParam(String code) {
        wmsBusiniessParamConfigService.flushConfigParam(code);
        return Result.ok("清理业务参数缓存成功");
    }

    /**
     * 根据业务参数获取值
     */
    @ApiOperation(value = "根据业务参数获取值", notes = "根据业务参数获取值")
    @RequestMapping(value = "/getBussinessValByCode", method = RequestMethod.GET)
    public Result<?> getBussinessValByCode(@RequestParam(value = "code") String code) {
        return Result.ok(wmsBusiniessParamConfigService.getBussinessValByCode(code));
    }
}
